// pages/login/forget/index.js
const app = getApp()
const my = require("../../../utils/MyUtils.js")
var log=require("../../../utils/log")
const MyUtils = require("../../../utils/MyUtils.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: 300, //验证码300s获取一次
    disabled: true, //是否可发验证码
    sendMg: "获取验证码",
    passW: null,
    passW2: null,
    errMsg: null,
    errMsg2: null,
    code: null,
    codeIn: null
  },
  userNameInput: function (e) {
    try {
      this.setData({
        userN: e.detail.value,
      })
    } catch (e) {
      wx.showToast({
        title: '手机号格式不对',
        icon: 'none'
      })
    }
  },
  toppd(e) {
    var msg = null
    var phone = e.detail.value;
    var disabled=true
    if (isNaN(Number(phone)) || phone.length < 11) {
      msg = "手机号格式有误"
    } else {
      disabled= false
    }
    this.setData({
      errMsg2: msg,
      disabled
    })
  },
  pd() {
    var msg = null
    if (this.data.passW == null || this.data.passW2 == null) {
      msg = "密码为空或格式不正确";
    } else if (this.data.passW != this.data.passW2) {
      msg = "两次密码不一致";
    }
    this.setData({
      errMsg: msg
    })
  },
  passWdInput: function (e) {
    if (e.currentTarget.dataset.id == '1') {
      this.setData({
        passW: e.detail.value
      })
    } else {
      this.setData({
        passW2: e.detail.value
      })
    }
  },
  returns() {
    wx.navigateTo({
      url: "../login"
    })
  },
  sendMsg() {
    var t = this
    my.request('/oa-applet/reset-pwd-code', "POST", {}, {
      phone: this.data.userN
    }, function (res) {
      if (res.data.code == 0) {
        t.setData({
          code: res.data.data.code
        })
      } else {
        t.setData({
          errMsg2: res.data.message
        })
      }
    })
    this.getCountDown()
  },
  getCountDown() { //验证码5分钟倒计时
    var time = parseInt(this.data.time);
    if (time <= 0) {
      this.setData({
        sendMg: "获取验证码",
        time: 300,
        disabled: false
      })

      // this.sendNum = 0;
      return false;
    } else {
      this.setData({
        time: parseInt(this.data.time) - 1,
        sendMg: "剩余" + this.data.time + "S",
        disabled: true
      })
      setTimeout(this.getCountDown, 1000);
    }
  },
  codeInput(e) {
    this.setData({
      codeIn: e.detail.value
    })
  },
  tijiao: function () {
    var t= this
    my.request('/oa-applet/check-code', "POST", {}, {
      code: t.data.codeIn,
      phone: t.data.userN
    }, function (res) {
      if (res.data.code == 0) {
        my.request('/oa-applet/reset-password', "POST", {
          tk: res.data.data.tk
        }, {
          phone: t.data.userN,
          pwd: t.data.passW2,
          'content-type': 'application/x-www-form-urlencoded'
        }, function (ress) {
          wx.hideLoading()
          if (ress.data.code == 0) {
            log.info(my.info( new Date(),t.data.userN,"重置了密码"))
            wx.showModal({
              cancelColor: 'cancelColor',
              content: '密码重置成功',
              success(res) {
                if (res) {
                  wx.navigateTo({
                    url: '../login',
                  })
                }
              }
            })
          } else {
            wx.showModal({
              cancelColor: 'cancelColor',
              content: ress.data.message
            })
          }
        })
      } else {
        this.setData({
          errMsg2: res.data.message
        })
      }
    })
  },
  show(e){
    // console.log(e)
    if(e.currentTarget.dataset.id=="new"){
      this.setData({
        show:true
      })
    }else{
      this.setData({
        show1:true
      })
    }
  },
  hide(e){
    // console.log(e)
    // if(e.currentTarget.dataset.id=="new"){
      this.setData({
        show:false,
        show1:false
      })
    // }else{
    //   this.setData({
    //     show1:true
    //   })
    // }
  },
  hideMes(){
    this.setData({
      errMsg2:false,
      errMsg:false
    })
  }

})